package com.example.network_android;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ClientActivity extends Activity implements OnClickListener {
	Client c;


		

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);
        
        Button b = (Button)findViewById(R.id.button_send);
        b.setOnClickListener(this);
        
        Bundle bundle = getIntent().getExtras();
        String ip = bundle.getString("ip");
        c = new Client(ip, 8888);
        
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_client, menu);
        return true;
    }

	public void onClick(View v) {
		final Handler h = new Handler();
		Thread t = new Thread(new Runnable(){

			public void run() {
				EditText t = (EditText)findViewById(R.id.editText1);
				final TextView tv = (TextView)findViewById(R.id.textView_answer);
				final Object answer = c.send(t.getText().toString());
				
				h.post(new Runnable(){

					public void run() {
						if (answer instanceof String){
							tv.setText((String)answer);
						} else {
							tv.setText("No response from server :-(");
						}
						
					}
					
				});
				
				
			}
			
		});
		t.start();

		
	}


}
